/**
@author - Gyanendra Singh/ Suresh Chandra Pal
@version - 1.0 28/Feb/2009
**/
function validateLoginForm (form) {
	if(form.username.value == "") {
		alert("Please enter value for Username.");
		form.username.focus();
		return false;
	} else {
		var check = userNameValidation(form.username);
		if ( check == 0) {
			alert("Please enter valid Username.");
			form.username.focus();
			return false;
		}
	}
	
  	if(form.password.value == "") {
		alert("Please enter value for Password.");
		form.password.focus();
		return false;
	}
	
	/////////////////////////////cookies code..
	manageCookies(form.username.value, form.password.value, form.enabled.checked);
	// coikies code end here
	
	validateLogin(form.username.value, form.password.value, form.login_type.value);	
	return false;	
}

function validateLogin(username, password, login_type) {
	var serverUrl = "../Admin";
	
	createRequest();
	if(ajaxRequest == null)
		return;
	
	ajaxRequest.onreadystatechange = checkServerResponse;
	ajaxRequest.open("POST", serverUrl, true);
	ajaxRequest.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
	ajaxRequest.send("method=login&username=" + username + "&password=" + password+ "&login_type=" + login_type);
	return false;	
}

function checkServerResponse() {
	if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
    	var response = ajaxRequest.responseText;
		
		if(response == 1) {	
    		document.location = "main.jsp";
    	} else {
    		document.getElementById("loginErrorPanel").innerHTML = response;
    	}
    } 
}


/////////////////////COOKIES MANAGEMENT/////////////////////
function manageCookies(username, password, enabled) {
	var enableCookie = (enabled ? '1' : '0')
	if(enableCookie == 1) {
		setCookie(username, password);
	} else {
		deleteCookie();
	}
}

function checkForCookies() {
	var arrayList = getCookie();
	if(arrayList != null) {
		arrayList = arrayList.split("$#$");
		document.getElementById("username").value = arrayList[0];
		document.getElementById("password").value = arrayList[1];
	}
}

var cookieName = 'rManager'; 

function setCookie(username, password) {
	var stateAsString = username + "$#$" + password;
	var expires = new Date();     //set new date object
	var path = "";
	var domain = "";
	var secure = "";
	expires.setTime(expires.getTime() + (1000 * 60 * 60 * 24 * 30));     //set it 30 days ahead 
	
  var curCookie = cookieName + "=" + escape(stateAsString) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie() {
  var dc = document.cookie;
  var prefix = cookieName + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
	var name = cookieName;
	var path = "";
	var domain = "";
  	if (getCookie()) {
	    document.cookie = name + "=" +
	    ((path) ? "; path=" + path : "") +
	    ((domain) ? "; domain=" + domain : "") +
	    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  	}
}

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function loginTypeSelection(changeLoginType) {

	var selectedLoginType = trim(changeLoginType);
	var lable_username = 'Roll Number:';
	var lable_password = 'DOB:';
	switch(selectedLoginType) {
		case 'Student':
			var lable_username = 'Roll Number:';
			var lable_password = 'DOB:';
			document.getElementById("span_password").innerHTML =
			'<input type="text" id="password" name="password" style="width:180px;color:#C1C1C1;" value="YYYY-MM-DD" />';
			break;
		case 'Administrator':
			var lable_username = 'Login Code:';
			var lable_password = 'Password:';
			document.getElementById("span_password").innerHTML =
				'<input type="password" id="password" name="password" style="width:180px;" />';
			break;
		case 'Evaluator':
			var lable_username = 'Login Code:';
			var lable_password = 'Password:';
			document.getElementById("span_password").innerHTML =
				'<input type="password" id="password" name="password" style="width:180px;" />';
			break;			
			
	}
	
	document.getElementById("lable_username").innerHTML = lable_username;
	document.getElementById("lable_password").innerHTML = lable_password;
	
	/*
	var searchValue = document.getElementById("searchValue").value;
	searchValue = trim(searchValue);
	if(searchValue == searchTextValue) {
		searchValue = '';
	}
	var params = 'searchValue=' + searchValue
				+ '&selectedYear=' + selectedYear	
				+ '&action=search';	
	
	
	var params = 'selectedYear=' + selectedYear
	
	loadContent(currentPage, params);*/
	return true;
}
